package com.nccsjz.back.section.utils;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import utils.FileUtils;

import com.nccsjz.pojo.Section;
import com.nccsjz.utils.CardException;

/**
 * LevelTwoUtils类包含了与二级栏目维护相关的方法
 * 
 * @author jason 2009-10-29
 * @since 1.0
 */
public class LevelTwoUtils {

	/**
	 * createLevelTwoStaticFile方法用于创建二级栏目对应的静态html文件
	 * 
	 * @param templateRealPathFileName	静态文件模板文件的物理路径
	 * @param staticRealPathFileName	目标文件的物理路径
	 * @param replaceFlags				替换标识
	 * @param replaceContent			替换内容
	 * @throws IOException
	 * @throws CardException
	 */
	public static void createLevelTwoStaticFile(
			String templateRealPathFileName, String staticRealPathFileName,
			String[] replaceFlags,String[] replaceContent) throws IOException, CardException {
		String templateFileContant = FileUtils
				.readFromFile(templateRealPathFileName);		
		for(int i=0;i<replaceFlags.length;i++){
			templateFileContant = templateFileContant.replace(replaceFlags[i], replaceContent[i]);			
		}
		//删除旧文件		
		FileUtils.deleteFile(staticRealPathFileName);
		//写入新文件
		FileUtils.saveFile(staticRealPathFileName, templateFileContant);
	}
	
	/**
	 * 生成leveltwos.js文件
	 * 
	 * @param levelTwoRealPathJsFileName
	 * @param levelTwos
	 * @throws CardException 
	 * @throws IOException 
	 */
	public static void createLevelTwoJsFile(String levelTwoRealPathJsFileName,List<Section> levelTwos) throws IOException, CardException{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("var arrLevelTwo=[");
		for(int i=0;i<levelTwos.size();i++){
			strBuf.append("[").append("\'");
			strBuf.append(levelTwos.get(i).getTitle());
			strBuf.append("\'").append(",");
			strBuf.append("\'");
			strBuf.append(levelTwos.get(i).getFileName());
			strBuf.append("\'").append("]");
			if(i<levelTwos.size()-1){
				strBuf.append(",");
			}			
		}
		strBuf.append("];");
		FileUtils.deleteFile(levelTwoRealPathJsFileName);
		FileUtils.saveFile(levelTwoRealPathJsFileName, strBuf.toString());
	}
}
